import { apiClient } from '@/lib/api-client';
import type { ApiResponse, User } from '@/lib/types/api';

interface UpdateUserRequest {
  username?: string;
  gender?: string;
  description?: string;
  birthdayAt?: Date;
}

export class UserService {
  // 获取用户基础数据
  static async getUserProfile(): Promise<ApiResponse<User>> {
    return await apiClient<null, User>('/auth/me', {
      method: 'GET',
    });
  }

  // 更新用户基础数据
  static async updateUserProfile(data: UpdateUserRequest): Promise<ApiResponse<User>> {
    try {
      const response = await apiClient<UpdateUserRequest, User>('/auth/me', {
        method: 'PUT',
        body: data,
      });

      if (response.status && response.data) {
        // 更新本地存储的用户信息
        const currentUser = localStorage.getItem('user');
        if (currentUser) {
          const updatedUser = {
            ...JSON.parse(currentUser),
            ...response.data,
          };
          localStorage.setItem('user', JSON.stringify(updatedUser));
        }
      }

      return response;
    } catch (error) {
      console.error('Update user profile error:', error);
      return {
        status: false,
        message: error instanceof Error ? error.message : '更新用户信息失败',
      };
    }
  }

  // 删除用户账号
  static async deleteUserAccount(): Promise<ApiResponse<void>> {
    try {
      const response = await apiClient<null, void>('/auth/me', {
        method: 'DELETE',
      });

      if (response.status) {
        // 清除本地存储
        localStorage.removeItem('token');
        localStorage.removeItem('user');
      }

      return response;
    } catch (error) {
      console.error('Delete user account error:', error);
      return {
        status: false,
        message: error instanceof Error ? error.message : '删除账号失败',
      };
    }
  }
} 